import React, { createContext, useContext, useCallback, useState } from 'react';
import { useQuery } from 'react-query';
import { Me } from 'api/queries';
import { isAccountActive, isFreeTrial } from "libs/utils";
import i18next from "libs/i18n";
import Storage from "libs/storage";
import { JWT_TOKEN, SUPER_JWT_TOKEN } from "config";
//import { Login, Register, Logout } from 'Utils/mutations';

// Simple promise to check tokens
const checkTokens = () => {
  return new Promise((resolve) => {
    const jwt = Storage.getItem(JWT_TOKEN);
    const superJwt = Storage.getItem(SUPER_JWT_TOKEN);
    resolve({
      hasToken: !!(jwt || superJwt),
      jwt,
      superJwt
    });
  });
};

const AuthContext = createContext(null);

export const AuthProvider = ({ children }) => {
  // Add state for all our auth values
  const [userData, setUserData] = useState(null);
  const [isAuthed, setIsAuthed] = useState(false);
  const [userRole, setUserRole] = useState(null);
  const [planType, setPlanType] = useState(null);
  const [authError, setAuthError] = useState(null);

  const { isLoading, refetch } = useQuery("Me", Me, {
    retry: false,
    onSuccess: async (responseData) => {
      // Check tokens before setting values
      const { hasToken } = await checkTokens();
      
      //alert('Query Success Data: ' + JSON.stringify(responseData));
      // Set language just like in HOC
      if (responseData?.data?.language) {
        i18next.changeLanguage(responseData.data.language);
      }
      // Update our state values
      setUserData(responseData.data);
      setUserRole(responseData.data?.role || null);
      setPlanType(responseData.data?.account?.planType || null);
      setAuthError(null);
      
      // Set authentication state based on token presence
      if (!hasToken) {
        setIsAuthed(false);
      } else {
        setIsAuthed(true);
      }
    },
    onError: (err) => {
      //alert('Query Error: ' + JSON.stringify(err));
      setUserData(null);
      setIsAuthed(false);
      setUserRole(null);
      setPlanType(null);
      setAuthError(err);
    }
  });

  // Add helper functions for role and access checking with safe data access
  const hasRole = useCallback((allowedRoles) => {
    if (!userData?.role || !Array.isArray(allowedRoles)) return false;
    return allowedRoles.includes(userData.role);
  }, [userData]);

  const hasPlanType = useCallback((allowedPlanTypes) => {
    if (!userData?.account?.planType || !Array.isArray(allowedPlanTypes)) return false;
    return allowedPlanTypes.includes(userData.account.planType);
  }, [userData]);

  const isActive = useCallback(() => {
    if (!userData?.account) return false;
    return isAccountActive(userData.account);
  }, [userData]);

  // Add isTrial function
  const isTrial = useCallback(() => {
    if (!userData?.account) return false;
    return isFreeTrial(userData.account);
  }, [userData]);

  // Construct value object using our state values
  const value = {
    user: userData,
    isLoading,
    error: authError,
    isAuthenticated: isAuthed,
    role: userRole,
    planType,
    hasRole,
    hasPlanType,
    isActive,
    isTrial, // Add the isTrial function to the context value
    refetch,
    // Add setters in case we need them elsewhere
    setUserData,
    setIsAuthed,
    setUserRole,
    setPlanType,
    setAuthError
  };

  return (
    <AuthContext.Provider value={value}>
      {children}
    </AuthContext.Provider>
  );
};

export const useAuth = () => {
  const context = useContext(AuthContext);
  if (context === undefined) {
    throw new Error('useAuth must be used within an AuthProvider');
  }
  return context;
};